package com.bc004346.travelus.Fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.DatePicker;

import com.bc004346.travelus.Model.Direction;
import com.bc004346.travelus.Model.Interface.IDatePickerCallback;
import com.bc004346.travelus.R;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by Renats on 9/29/2014.
 * Date picker dialog
 */
public class DatePickerFragment extends DialogFragment {

    public static final String EXTRA_DATE =
            "com.bc004346.travelus.book_travel.date";
    public static final String EXTRA_DIRECTION =
            "com.bc004346.travelus.bbok_travel.direction";

    private Date mDate;
    private Direction mDirection;
    private IDatePickerCallback mCallback;

    public static DatePickerFragment newInstance(Date date, Direction direction) {
        Bundle args = new Bundle();
        args.putSerializable(EXTRA_DATE, date);
        args.putSerializable(EXTRA_DIRECTION, direction);

        DatePickerFragment f = new DatePickerFragment();
        f.setArguments(args);
        return f;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDirection = (Direction) getArguments().getSerializable(EXTRA_DIRECTION);
        mDate = (Date) getArguments().getSerializable(EXTRA_DATE);
        Calendar c = Calendar.getInstance();
        c.setTime(mDate);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        View v = getActivity().getLayoutInflater()
                .inflate(R.layout.dialog_date, null);

        DatePicker datePicker = (DatePicker) v.findViewById(R.id.dialog_date_datePicker);
        datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int month, int day) {
                mDate = new GregorianCalendar(year, month, day).getTime();
                getArguments().putSerializable(EXTRA_DATE, mDate);
            }
        });
        return new AlertDialog.Builder(getActivity())
                .setView(v)
                .setTitle("Date of flight")
                .setPositiveButton(android.R.string.ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialogInterface, int i) {
                                mCallback.onDatePicked(mDate, mDirection);
                            }
                        })
                .create();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mCallback = (IDatePickerCallback) activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallback = null;
    }
}
